//
//  SearchResultsTableViewController.swift
//  SCUTWiki
//
//  Created by Wu Weisong on 2020/12/20.
//  Copyright © 2020 wwszhw. All rights reserved.
//

import UIKit

class SearchResultsTableViewController: UITableViewController, StarDelegate {

    let cellIdentifer = "searchResultCell"
    var sqlManager = SQLiteManager.shareInstance
    var articlesArray = [ArticleModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifer)
    }
    
    // MARK: - Table view data source
    func searchArticles(title:String) -> Void {
        articlesArray.removeAll()
        if sqlManager.openDB(){
            if let articles = sqlManager.execQuerySQL(sql: "select * from articles where title like '%\(title)%'"){
                for (_,row) in articles.enumerated(){
                    articlesArray.append(ArticleModel(rowData: row))
                }
            }else{
                articlesArray.append(ArticleModel(id:-1, title: "暂无该内容的无数据", content: "暂无该内容的无数据", type_id: -1, collected: false))
            }
        }
        sqlManager.closeDB()
        self.tableView.reloadData()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return articlesArray.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer, for: indexPath)
        cell.selectionStyle = .none
        cell.textLabel?.text=articlesArray[indexPath.row].title
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let articleView = storyboard.instantiateViewController(withIdentifier: "ArticleViewController") as! ArticleViewController//因为vc的视图是通过storyboard创建的，所以要通过这种方式创建vc实例，否则会出现黑屏
        articleView.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .plain, target: articleView, action: #selector(ArticleViewController.backClick))//添加一个返回按钮
        
        //类似prepare先传参
        articleView.articleObject = articlesArray[indexPath.row]
        articleView.starDelegate = self
        //因为articleview在storyboard中是由navigation管理的，所以这里也要用一个nav来管理，以此让收藏按钮可以显示
        let navigationController = UINavigationController(rootViewController: articleView)
        //页面跳转
        self.present(navigationController, animated: true, completion: nil)
    }
    
    func changeCollection(collected: Bool) {
        if let select_index = self.tableView.indexPathForSelectedRow{
            if sqlManager.openDB(){
                if sqlManager.execUpdateSQL(sql: "update articles set collected=\(collected) where id=\(articlesArray[select_index.row].id)"){
                    print("search tableview 更改db收藏")
                }
            }
            sqlManager.closeDB()
            articlesArray[select_index.row].collected = collected
        }
    }
    
}
